<style>
    #user-add {
        padding: 20px 25px 25px 0;
    }
</style>
<div class="layui-fluid" id="user-add">
    <form class="layui-form" action="" lay-filter="user-add-form">
        <div class="layui-form-item" id="div3" style="width:450px;">
            <br><span class="personattr" style="display: inline;">批量导入用户信息：</span>
            <input type="hidden" name="excelUrl"  id="excelUrl"/>
            <span id="imgTitle" style="color: red ">上传excel表格前,请先下载excel模板</span>
            <input type="file" name="logoFile" id="logoFile" onchange="getUrll(this)" class="selectedLogoImgId" lay-filter="logoFile">
            <input type="text" name="excelUrlShow" id="excelUrlShow" style="width: 315px;">
        </div>
        <div class="layui-form-item febs-hide">
            <button class="layui-btn" lay-submit="" lay-filter="user-add-form-submit" id="submit"></button>
            <button type="reset" class="layui-btn" id="reset"></button>
        </div>
    </form>
</div>

<script>
    layui.use(['febs', 'form', 'validate', 'xmSelect'], function () {
        let $ = layui.$,
            febs = layui.febs,
            layer = layui.layer,
            form = layui.form,
            xmSelect = layui.xmSelect,
            validate = layui.validate,
            dataPermissionXmlSelect,
            roleXmSelect,
            deptXmlSelect;

        form.verify(validate);
        form.render();


        /**
         * 点击预览，导入excel
         */
        form.on("input(logoFile)", function (obj) {
            console.log(obj);
            var f=$(obj).val();
            if(f == null || f ==undefined || f == ''){
                return false;
            }
            if(!/\.(?:xls|xlsx)$/.test(f))
            {
                alert("类型必须是excel表格(.xls|xlsx)格式")
                $(obj).val('');
                return false;
            }
            var data = new FormData();
            $.each($(obj)[0].files,function(i,file){
                data.append('file', file);
            });
            $.ajax({
                type: "POST",
                url: "entranceScore/getFileUrl",
                data: data,
                cache: false,
                contentType: false,    //不可缺
                processData: false,    //不可缺
                dataType:"json",
                success: function(suc) {
                    console.log(suc);
                    // if(suc.code==0){
                    //     $("#excelUrl").val(suc.message);//将地址存储好
                    //     $("#excelUrlShow").val(suc.message);//显示excel
                    // }else{
                    //     alertLayel("上传失败");
                    //     $("#url").val("");
                    //     $(obj).val('');
                    // }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    alert("上传失败，请检查网络后重试");
                    $("#url").val("");
                    $(obj).val('');
                }
            });
        });
        function getUrll(obj){//用于进行excel上传，返回地址
            var a=$('#logoFile').val();
            console.log(a);
            console.log(obj.val());
        }

        dataPermissionXmlSelect = xmSelect.render({
            el: '#user-add-data-permission',
            model: {label: {type: 'text'}},
            tree: {
                show: true,
                strict: false,
                showLine: false,
                clickCheck: true,
                expandedKeys: [-1],
            },
            name: 'deptIds',
            theme: {
                color: '#52c41a',
            },
            prop: {
                value: 'id'
            },
            height: 'auto'
        });

        febs.get(ctx + 'dept/select/tree', null, function (data) {
            deptXmlSelect.update(data)
            dataPermissionXmlSelect.update(data)
        });

        roleXmSelect = xmSelect.render({
            el: '#user-add-role',
            toolbar: {show: true},
            name: 'roleId',
            theme: {
                color: '#52c41a',
            },
            prop: {
                name: 'roleName',
                value: 'roleId'
            },
            data: []
        });

        febs.get(ctx + 'role', null, function (data) {
            roleXmSelect.update({
                data: data.data,
                autoRow: true,
            })
        });

        form.on('submit(user-add-form-submit)', function (data) {
            if (!data.field.roleId) {
                febs.alert.warn('请选择用户角色');
                return false;
            }
            febs.post(ctx + 'user', data.field, function () {
                layer.closeAll();
                febs.alert.success('新增用户成功，初始密码为 1234qwer');
                $('#febs-user').find('#query').click();
            });
            return false;
        });
    });
</script>